package com.UtilsPak.localization;

import org.jdom.Element;
import com.UtilsPak.xml.XmlManager;

import javax.faces.context.FacesContext;

/*
 * Created by IntelliJ IDEA.
 * User: mchebanenko
 * Date: 28-Dec-2010
 */

public class LocalizationManager {
    private static LocalizationContext lContext;
    private static FacesContext fc;


    public static FacesContext getFc() {
        return fc;
    }

    public static void setFc(FacesContext fc) {
        LocalizationManager.fc = fc;
    }

    public static String getLocalized(String localizedConstant, String language) {
        lContext = new LocalizationContext();
        if (lContext.isEmpty()) {
            reloadLocalizations();
        }
        return lContext.getLocalized(localizedConstant, language);
    }

    private static void reloadLocalizations() {
       
        try {
            Element root = XmlManager.getRootElement(fc.getExternalContext().getResource("/WEB-INF/Messages.xml"));
            lContext.loadLocalizations(root);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
